
import os
import cv2
import sys
import numpy as np

class CMGet:
    def __init__(self):
        self.path = "."
    
class CMGetImages(CMGet):
    def read_images(self, path, sz=None):
        c = 0
        x, y = [], []

        for dirname, dirnames, filenames in os.walk(path):
            for subdirname in dirnames:
                subject_path = os.path.join(dirname, subdirname)
                for filename in os.listdir(subject_path):
                    try:
                        if (filename == ".directory"):
                            continue
                        filepath = os.path.join(subject_path, filename)
                        im = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)

                        if (sz is not None):
                            im = cv2.resize(im, (200, 200))
                        x.append(np.asarray(im, dtype = np.uint8))
                        y.append(c)
                    except:
                        print("Unexpected Error:", sys.exc_info()[0])
                        raise
                c = c + 1
        
        return [x, y]
        